package com.alit.face.koala.mq.apacheapollo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MQTTSender {
  private static MQTTService mqttService = null;

  static {
    mqttService = new MQTTService("litong", new MessageListener() {

      @Override
      public void mqttConnection() {
        log.info("连接成功");
      }

      @Override
      public void mqttClose() {
        log.info("连接关闭");
      }

      @Override
      public void handleMessage(String topic, String message) {
        System.out.println("收到boker返回的消息");
        System.out.println("topic:" + topic + ",message:" + message);
      }
    });
  }

  // 对外提供初始化的方法,在程序启动时进行初始化,执行之后,静态代码块会自动执行
  public static void init() {

  }

  public static boolean send(String topic, String message) {
    return mqttService.sendMessage(topic, message, 2);
  }

  public static void main(String[] args) {
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    MQTTSender.send("vp3", "发送 床前明月光，疑是地上霜。\r\n" + "举头望明月，低头思故乡。");
  }
}